package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanagelimit;
import com.example.demo.handle.LimitListParseHandler;
import com.example.demo.mapper.PersonnelmanagelimitMapper;
import com.example.demo.service.PersonnelmanagelimitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-23
 */
@Service
public class PersonnelmanagelimitServiceImpl extends ServiceImpl<PersonnelmanagelimitMapper, Personnelmanagelimit> implements PersonnelmanagelimitService {

    @Autowired
    private PersonnelmanagelimitMapper personnelmanagelimitMapper;

    @Override
    public Integer insertLimitData(String limitXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            LimitListParseHandler limitListParseHandler = new LimitListParseHandler();
            try {
                // 读取xml文件
                parser.parse(limitXml, limitListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + limitListParseHandler.getPersonnelmanagelimitArrayList().size() + "个节点被存储");
            for (Personnelmanagelimit personnelmanagelimit : limitListParseHandler.getPersonnelmanagelimitArrayList()) {
                System.out.println(personnelmanagelimit.getCsMineCode());
                System.out.println(personnelmanagelimit.getCsDataTime());
                System.out.println(personnelmanagelimit.getPsAlarmType());
                System.out.println(personnelmanagelimit.getPsAreaCode());
                System.out.println(personnelmanagelimit.getPsAreaEntertime());
                System.out.println(personnelmanagelimit.getPsStationCode());
                System.out.println(personnelmanagelimit.getPsStationEntertime());
                System.out.println(personnelmanagelimit.getPsUndergroundRealnum());
                System.out.println(personnelmanagelimit.getPsAreaRealnum());
                System.out.println(personnelmanagelimit.getPsAlarmStime());
                System.out.println(personnelmanagelimit.getPsAlarmEtime());
                System.out.println(personnelmanagelimit.getPsPersonCard());
                System.out.println(personnelmanagelimit.getPsMineEntertime());
                System.out.println("----finish----");
                personnelmanagelimitMapper.insertLimitData(personnelmanagelimit.getCsMineCode(),personnelmanagelimit.getCsDataTime(),personnelmanagelimit.getPsAlarmType(),personnelmanagelimit.getPsAreaCode(),personnelmanagelimit.getPsAreaEntertime(),personnelmanagelimit.getPsStationCode(),personnelmanagelimit.getPsStationEntertime(),personnelmanagelimit.getPsUndergroundRealnum(),personnelmanagelimit.getPsAreaRealnum(),personnelmanagelimit.getPsAlarmStime(),personnelmanagelimit.getPsAlarmEtime(),personnelmanagelimit.getPsPersonCard(),personnelmanagelimit.getPsMineEntertime());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
